﻿// credit: adapted from Josh Smith (http://msdn.microsoft.com/en-us/magazine/dd419663.aspx)
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Input;

namespace Results
{
    public class RelayCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public RelayCommand(Action<object> executeAction)
        {
            _executeAction = executeAction;
        }

        public RelayCommand(Action<object> executeAction, Predicate<Object> canExecute)
        {
            _canExecute = canExecute;
            _executeAction = executeAction;
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecute != null)
            {
                return _canExecute(parameter);
            }

            return true;
        }

        public void UpdateCanExecuteState()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, new EventArgs());
            }
        }

        public void Execute(object parameter)
        {
            if (_executeAction != null)
            {
                _executeAction(parameter);
            }

            UpdateCanExecuteState();
        }

        Predicate<Object> _canExecute = null;
        Action<Object> _executeAction = null;
    }
}
